#include "edgeitem.h"



EdgeItem::EdgeItem(const QLineF &line):QGraphicsLineItem(line)
{

}

EdgeItem::EdgeItem( NodeItem *from, NodeItem *to)
{
    n1 = from;
    n2 = to;
    n1->addEdge(this);
    n2->addEdge(this);
    updatePosition();

    pen_def =  QPen(QColor(0,0,0,255), 20,  Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
    setDefaultStyle();

}

void EdgeItem::updatePosition()
{
    QPointF p1 = n1->scenePos();
    QPointF p2 = n2->scenePos();
    QLineF  l(p1,p2);
    setLine(l);
    update();
}

QPainterPath EdgeItem::shape() const
{
    QPainterPath path;
    QPainterPathStroker stroker;
    stroker.setWidth(qMax(4, pen().width() + 3));
    path.moveTo(line().p1());
    path.lineTo(line().p2());
    return stroker.createStroke(path);
}

QRectF EdgeItem::boundingRect() const
{
    return shape().boundingRect();
}

void EdgeItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    painter-> setRenderHint (QPainter:: Antialiasing);
    painter->strokePath(shape(), pen_current);
    QGraphicsLineItem::paint(painter, option, widget);
}

NodeItem *EdgeItem::getNode1()
{
    return n1;
}

NodeItem *EdgeItem::getNode2()
{
    return n2;
}

NodeItem *EdgeItem::getNodeOther(NodeItem *node)
{
    if(node == n1){
       return n2;
    }else{
       return n1;
    }
}


//void EdgeItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
//{
//    QGraphicsLineItem::paint(painter,option,widget);
//}


void EdgeItem::setDefaultStyle()
{
    pen_current = pen_def;
    edgeColor = QColor(0,0,0,255);
    edgeWidth = 20;

}


void EdgeItem::setEdgeColor(QColor c)
{
   edgeColor = c;
   pen_current =  QPen(edgeColor, edgeWidth,  Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
}

void EdgeItem::setEdgeWidth(int w)
{
    edgeWidth = w;
    pen_current =  QPen(edgeColor, edgeWidth,  Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
}
